package inject;
import java.io.File;
import java.net.URL;
import com.google.inject.AbstractModule;
import com.google.inject.Singleton;

public class DefaultModule extends AbstractModule{

	public Class<?> clazz;
	public DefaultModule(Class<?> clazz){
		this.clazz = clazz;
	}
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	protected void configure(){
		String packageName = (clazz.getPackage().getName()+"Impl").replace(".", "/");
		ClassLoader classloader = Thread.currentThread().getContextClassLoader();  
        URL url = classloader.getResource(packageName); 
        File dir = new File(url.getFile());
        if (!dir.exists()) {  
            return;  
        }
        for (File f : dir.listFiles()) {  
            String name = f.getName();  
            if (name.endsWith(".class")) {  
				Class classImpl;
				try {
					classImpl = Class.forName(packageName.replace( "/", ".") + "." + name.substring(0, name.length() - 6));
					if(clazz.isAssignableFrom(classImpl)){
						 bind(clazz).to(classImpl).in(Singleton.class);
					}
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				}
				
            }  
        } 
	}
}
